﻿using SharPy.Parser;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace SharPy.GameMenu
{
    /// <summary>
    /// Class to create a list of menu options from a text block 
    /// So far it supports only label jumps or oneliner after option
    /// TODO: change navigation pattern to PageFunction or Navigate
    /// </summary>
    public static class clMenuClass
    {
        public static List<clMenuOption> optList = new List<clMenuOption>();
        public static string sSayStatement;

        //public static clMenuClass(int pLineNum)
        //{
        //    buildMenu(pLineNum);
        //}

        public static async System.Threading.Tasks.Task<clMenuOption[]> buildMenu(int plineNum)
        {
            flushMenuClass();

            string patternOption = ":$";
            string patternText = "\"([^\"]*)\"$";

            clTextLine[] menuBlock = await clText.getBlock(plineNum);

            int nLine = 0;
            int index = 0;

            foreach (clTextLine s in menuBlock)
            {
                // First line, skip it
                if (s.sLine == "menu:" && nLine == 0) ;

                // Second line could be a say-statement 
                else if (nLine == 1 && Regex.IsMatch(s.sLine, patternText))
                    sSayStatement = s.sLine;
                                                     
                else if (Regex.IsMatch(s.sLine, patternOption) &&
                            (s.nIndention == menuBlock[0].nIndention)) //For in-block menus
                {
                    //We return the number of next line as menu click action
                    optList.Add(new clMenuOption());
                    string name = Regex.Matches(s.sLine, @"""(.*)""")[0].Groups[1].Value;
                    optList[index].sName = clText.cleanString(name);
                    optList[index].objCommand = new clCommand(clCommand.Name.JumpToLine
                                                            , (s.nLineNo + 1).ToString());
                    index++;
                }

                else ;
                nLine++;
            }

            return optList.ToArray();
        }

        private static void flushMenuClass()
        {
            optList.Clear();
            sSayStatement = null;
        }
    }


    /// <summary>
    /// Simple instance of a menu option
    /// </summary>
    public class clMenuOption
    {
        public string sName;
        public clCommand objCommand;
    }

}
